package cn.com.huzo.service.purchase.impl;

import cn.com.huzo.model.entities.purchase.PoLinesMrpEntity;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import cn.com.huzo.service.purchase.PoLinesManager;
import cn.com.huzo.service.purchase.PoLinesMrpManager;
import cn.com.huzo.service.purchase.PoLinesMrpLinesManager;
import cn.com.huzo.repository.purchase.PoHeaderRepository;
import cn.com.huzo.repository.purchase.PoLinesRepository;
import cn.com.huzo.repository.purchase.PoLinesMrpLinesRepository;
import cn.com.huzo.repository.purchase.PoLinesMrpRepository;

/**
 * 采购订单明细--引入调达表管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("PoLinesMrpManager")
public class PoLinesMrpManagerImpl extends GenericManagerImpl<PoLinesMrpEntity, String>
        implements PoLinesMrpManager {

    @Resource(name = "PoHeaderRepository")
    private PoHeaderRepository _dao_header;

    @Resource(name = "PoLinesRepository")
    private PoLinesRepository _dao_lines;

    @Resource(name = "PoLinesMrpRepository")
    private PoLinesMrpRepository _dao_lines_mrp;

    @Resource(name = "PoLinesMrpLinesRepository")
    private PoLinesMrpLinesRepository _dao_lines_mrp_lines;

    @Resource(name = "PoLinesMrpLinesManager")
    private PoLinesMrpLinesManager _manager_lines_mrp_lines;

    @Resource(name = "PoLinesManager")
    private PoLinesManager _manager_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> insertLines(Long headerId, Long updatedBy, List<Long> mrpHeaderIdList, List<String> mrpLinesIdList,
            Short separateFlagByPartOfNeed, Integer limit02, Integer limit04, String poNo) throws Exception {

        // 第一步，增加明细（引入的物料需求计划表头文件）
        List<PoLinesMrpEntity> itemList = new ArrayList();
        for (int i = 0; i < mrpHeaderIdList.size(); i++) {
            PoLinesMrpEntity instance = new PoLinesMrpEntity();
            instance.setHeaderId(headerId);
            instance.setMrpHeaderId(mrpHeaderIdList.get(i));
            instance.setCreatedBy(updatedBy);
            instance.setUpdatedBy(updatedBy);
            itemList.add(instance);
        }
        _dao_lines_mrp.batchInsert(itemList);

        // 第二步，增加明细（引入的物料需求计划明细文件，即材料需求）
        Integer rowsAffected = _dao_lines_mrp_lines.insertLines(headerId, updatedBy, mrpLinesIdList, separateFlagByPartOfNeed);

        // 第三步，插入订单明细
        if (rowsAffected > 0) {
            _dao_lines.insertByImportedMrpLines(headerId, updatedBy, mrpLinesIdList, separateFlagByPartOfNeed);
            _dao_lines.delete_for_sync_lines(headerId); // 删除没有产生订单内容的无效引入

            // 审计日志
            writeAuditLog(updatedBy, "采购管理→采购订单引入调达表", "订单号：" + poNo, "引入");
        }

        // 第四步：删除没有产生订单内容的无效引入
        _dao_lines_mrp_lines.deleteInvalidRow(headerId);
        _dao_lines_mrp.deleteInvalidRow(headerId);

        // 第五步：得到返回值
        Map<String, Object> _result = new HashMap();

        Map<String, Object> criterion = new HashMap();
        criterion.put("HeaderId", headerId);      // 当前为第一页
        criterion.put("PageIndex", 1);            // 当前为第一页

        // 得到引入的调达表明细信息
        criterion.put("Limit", limit02);
        _result.put("LinesMrpList", _dao_lines_mrp.fetchByHeaderId(headerId));
        _result.put("LinesMrpLinesList", _manager_lines_mrp_lines.fetchForPage(criterion));

        // 得到新的采购订单明细
        criterion.put("Limit", limit04);
        _result.put("PoLinesList", _manager_lines.fetchForPage(criterion));
        _result.put("PoMaterialList", _dao_lines.fetchPoMaterialList(headerId));

        return _result;
    }
}
